<?php
namespace Common\Model;
use Think\Model;

class AdminModel extends Model {
	private $_db='';
	public function __construct(){
		$this->_db=M('admin');
	}
	public function getAdminByUsername($username) {
		$ret =	$this->_db->where('username="'.$username.'"')->find();
		return $ret;
	}
	public function getAdminUserList ($data,$page,$pageSize=10) {
		$data['status']=array('neq',-1);
		$offset =($page-1)*$pageSize;
		$list = $this->_db->where($data)
		->order('admin_id desc')
		->limit($offset,$pageSize)
		->select();
		return $list;
	}
	
	public function getMenusCount ($data=array()){
		$data['status']=array('neq',-1);
		return $this->_db->where($data)->count();
	}
	
	public function insert($data=array()) {
		if(!$data || !is_array($data)){
			return 0;
		}
		return $this->_db->add($data);
	}
	
	public function find ($id){
		if(!$id || !is_numeric($id)){
			return array();
		}
		return $this->_db->where('admin_id='.$id)->select();
	}
    public function getUserById ($id){
        if(!$id || !is_numeric($id)){
            return array();
        }
        return $this->_db->where('admin_id='.$id)->select();
    }
	
	public function updataById ($id,$data) {
		if( !$id || !is_numeric($id) ){
			throw_exception('ID不合法');
		}
		if( !$data || !is_array($data) ){
			throw_exception('更新的数据不合法');
		}
		return $this->_db->where('admin_id='.$id)->save($data);
	}
	
	public function updataStatusById ($id,$status) {
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		if(!is_numeric($status) || !$status){
			throw_exception("状态不合法");
		}
		$data['status']=$status;
		return $this->_db->where('admin_id='.$id)->save($data);
	}
}
